//
//  CZcommentModel.swift
//  GZWeibo007
//
//  Created by JLHong on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class CZcommentModel: NSObject {
   
    /// 用于显示正在加载数据cell
    var loading = false
    
    /// 评论的ID
    var id: Int = 0
    
    /// 评论创建时间
    var created_at: String?
    
    /// 评论作者的用户信息字段
    var user: CZUser?
    
    var status: CZStatus?
    
    var rowHeight: CGFloat?
    
    /// 内容
    var text: String?
    
    init(loading: Bool) {
        self.loading = loading
        
        super.init()
    }
    
    /// 字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        
        // KVC字典转模型
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String: AnyObject] {
                user = CZUser(dict: dict)
            }
            return
        }
        
        if key == "status"{
            if let dict = value as? [String: AnyObject]{
                status = CZStatus(dict: dict)
            }
        }
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        let properties = ["id", "created_at", "user"]
        
        return "\n\t:评论模型: \(dictionaryWithValuesForKeys(properties))"
    }
    

    class func loadcommentData(since_id: Int, max_id: Int, statuId: Int,finished:(commentData: [CZcommentModel]? , error: NSError?) -> ()) {
        
           CZNetworkTool.sharedInstance.loadCommentMeodel(since_id, max_id: max_id, statuId: statuId) { (result, error) -> () in
            
            if error != nil {
                
                return
            }
            
            // 字典转模型
            var comments = [CZcommentModel]()
            // 解析 comments 里面的评论模型
            if let array = result?["comments"] as? [[String: AnyObject]] {
                for dict in array {
                    let comment = CZcommentModel(dict: dict)
                    
                    comments.append(comment)
                }
                
              finished(commentData: comments, error: nil)
         
            } else {
                finished(commentData: nil, error: error)
            }
        }
    }
     
}

